#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "historylist.h"
#include"historyfocus.h"
#include "arcwidget.h"
#include "databasemanager.h"
#include <QString>
#include <QDate>
#include <QLabel>
#include <QPaintEvent>
#include <QVBoxLayout>

MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QPixmap  img(":/images/imgs/agna_proud.png");
    QPixmap  img2(":/images/imgs/agna_greet.png");
    QPixmap scaledimg = img.scaled(55, 55, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->pushButton->setIcon(QIcon(scaledimg));
    ui->pushButton_3->setIcon(QIcon(scaledimg));
    ui->pushButton->setIconSize(QSize(35, 35));
    ui->pushButton_3->setIconSize(QSize(35, 35));
    ui->pushButton->setStyleSheet("color: #808080;");
    ui->pushButton_3->setStyleSheet("color: #808080;");
    ui->frame->setStyleSheet("background-color: white;");
}

MainWindow::~MainWindow(){
    delete ui;
}

//生成历史数据表
void MainWindow::history_list()
{
    historylist *m_historylist=new historylist();
    m_historylist->sethistorylist();
    m_historylist->show();
}

//选中某天的数据生成图表
void MainWindow::select_history_of_oneday(const QDate &date)
{
    QSqlQuery query(DatabaseManager::getDatabase());

    QString sqlselect = "SELECT * FROM history WHERE date = ? AND user = ?;";
    query.prepare(sqlselect);
    query.addBindValue(date);
    query.addBindValue(DatabaseManager::getUID());
    QString dateString = date.toString("yyyy-MM-dd");

    type_name_Data.clear();
    duration_Data.clear();
    bool hasData = false;
    query.exec();
    while (query.next()) {
        int id = query.value("id").toInt();
        QString task_name = query.value("task_name").toString();
        QString task_type = query.value("task_type").toString();
        int duration = query.value("duration").toInt();
        QString date = query.value("date").toString();
        qDebug() << "ID:" << id << "Task Name:" << task_name << "Task Type:" << task_type << "Duration:" << duration << "Date:" << date;

        type_name_Data.append(qMakePair(id, task_name));
        duration_Data.append(qMakePair(id, duration));
        hasData = true;
    }

    if (!hasData)
    {
        // 如果没有数据，显示一个带有图片的消息
        QWidget *noDataWidget = new QWidget();
        noDataWidget->setWindowTitle(tr("番茄钟"));
        QVBoxLayout *layout = new QVBoxLayout(noDataWidget);

        QLabel *emptyLabel = new QLabel(noDataWidget);
        QPixmap pixmap(":/images/imgs/no_data.png"); // 图片路径
        emptyLabel->setPixmap(pixmap.scaledToWidth(300, Qt::SmoothTransformation));
        emptyLabel->setAlignment(Qt::AlignCenter);
        layout->addWidget(emptyLabel);

        noDataWidget->setLayout(layout);
        noDataWidget->resize(600, 600);
        noDataWidget->show();
    }
    else
    {
        ArcWidget *arcWidget = new ArcWidget();

        for (const auto &pair : type_name_Data) {
            qDebug() << "Task ID:" << pair.first << ", Task Name:" << pair.second;
            arcWidget->type_name_Map.insert(pair.first, pair.second);
        }
        for (const auto &pair : duration_Data) {
            qDebug() << "Task ID:" << pair.first << ", Duration:" << pair.second;
            arcWidget->duration_Map.insert(pair.first, pair.second);
        }
        arcWidget->update();
        QWidget *window = new QWidget();
        window->setWindowTitle(tr("番茄钟"));
        QVBoxLayout *layout = new QVBoxLayout(window);
        layout->addWidget(arcWidget);
        window->setLayout(layout);
        window->resize(600, 600);

        window->show();
    }
    this->close();
}

void MainWindow::on_pushButton_clicked()
{
    history_list();
    this->close();
}

void MainWindow::on_pushButton_3_clicked()
{
    historyfocus*m_focus=new historyfocus;
    this->close();
    m_focus->show();
}

